//
//  TwitterCell.m
//  YaTc
//
//  Created by Roberto on 17/02/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TwitterCell.h"
#import "TwitterPost.h"

#define HORIZ_TEXT_PADDING 4
#define VERT_TEXT_PADDING 4
#define HORIZ_IMG_PADDING 10
#define VERT_IMG_PADDING 11

@implementation TwitterCell
@synthesize userLoggedIn;

- (id) initWithUserLoggedIn:(NSString *)user
{
	if ( ![super init] )
		return nil;
	[self setUserLoggedIn:user];
	
	return self;
}

- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
{
	NSBezierPath *background;
	TwitterPost *post = [self objectValue];
	
	[[NSColor blackColor] set];	
	[NSBezierPath setDefaultLineJoinStyle:NSRoundLineJoinStyle];
	background = [NSBezierPath bezierPathWithRect:NSMakeRect(cellFrame.origin.x + 2, cellFrame.origin.y+2, cellFrame.size.width-3, cellFrame.size.height-5)];
	[background stroke];
	if ( [post isDirectMessage] )
	{
		[[NSColor colorWithDeviceRed:0.7 green:0.9 blue:0.7 alpha:0.8] set];
	}
	else
	{
		if ( [[post message] rangeOfString:userLoggedIn].location == NSNotFound)
		{
			[[NSColor colorWithDeviceRed:0.6 green:0.6 blue:0.6 alpha:0.8] set];
		}
		else
		{
			[[NSColor colorWithDeviceRed:0.9 green:0.7 blue:0.7 alpha:0.8] set];
		}
	}
	[NSBezierPath fillRect:NSMakeRect(cellFrame.origin.x+2, cellFrame.origin.y+2, cellFrame.size.width-3, cellFrame.size.height-5)];
	NSDictionary* textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor blackColor], 
									NSForegroundColorAttributeName, [NSFont systemFontOfSize:12], NSFontAttributeName, nil];
	
	[[post message] drawInRect:NSMakeRect(cellFrame.origin.x + cellFrame.size.height + HORIZ_TEXT_PADDING, cellFrame.origin.y + VERT_TEXT_PADDING, cellFrame.size.width - 65, cellFrame.size.height) 
				withAttributes:textAttributes];
	NSImage *image = [[NSImage alloc] initByReferencingURL:[post imageLocation]];
	//[image drawAtPoint:NSMakePoint(cellFrame.origin.x, cellFrame.origin.y) fromRect:NSMakeRect(0,0, cellFrame.size.height, cellFrame.size.height) 
	//		 operation:NSCompositeCopy fraction:1];
	[image setFlipped:YES];
	[image drawInRect:NSMakeRect(cellFrame.origin.x+5, cellFrame.origin.y+5, cellFrame.size.height-HORIZ_IMG_PADDING, cellFrame.size.height - VERT_IMG_PADDING) 
			 fromRect:NSMakeRect(0,0, [image size].width, [image size].height) operation:NSCompositeCopy fraction:1.0];
	NSString *userLocation;
	userLocation = [[NSString alloc] initWithFormat:@"Por: %@ en %@ -- %@", [post user], [post userLocation], [[post messageDate] descriptionWithCalendarFormat:@"%a %b %e %Y, %H:%M:%S" timeZone:nil locale:nil]];
	textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor colorWithDeviceRed:0.2 green:0.2 blue:0.2 alpha:1.0], 
									NSForegroundColorAttributeName, [NSFont systemFontOfSize:10], NSFontAttributeName, nil];
	[userLocation drawAtPoint:NSMakePoint(cellFrame.origin.x + cellFrame.size.height, cellFrame.origin.y+cellFrame.size.height - 17) 
			   withAttributes: textAttributes];

	
	
}

- (void)setObjectValue:(id )object {
	id oldObjectValue = [self objectValue];
	if (object != oldObjectValue) {
		[object retain];
		[oldObjectValue release];
		[super setObjectValue:[NSValue valueWithNonretainedObject:object]];
	}
}

- (id)objectValue {
	return [[super objectValue] nonretainedObjectValue];
}
@end
